using System;
using System.Collections.Generic;
using System.Data;
using Ninject;
using RentalOfCars.Domain.Entities;
using RentalOfCars.Server.Domain.ClientServerInterface;

namespace RentalOfCars.Client.Presentation
{
    public class MainWindowPresenter
    {
        private readonly IKernel _kernel;
        private readonly IDbAccessSession _session;
        private readonly IMainWindowView _view;
        private IList<IClient> _clients;
        private IList<IEmployee> _employees;

        public MainWindowPresenter(IMainWindowView view, IDbAccessSession session, IKernel kernel)
        {
            _view = view;
            _session = session;
            _kernel = kernel;

            _view.OnAddClient = OnAddClient;
            _view.OnDeleteClient = OnDeleteClient;
            _view.OnRegularClients = OnRegularClients;
            _view.OnBestClients = OnBestClients;
            _view.OnFineClietns = OnFineClietns;
            _view.OnGetEmployees = OnGetEmployees;
            _view.OnHireEmployee = OnHireEmployee;
            _view.OnFireEmployee = OnFireEmployee;

            UpdateClientsTable();
        }

        private void OnFireEmployee()
        {
            if (_view.SelectedEmployeeIndex == -1)
            {
                return;
            }

            var messenger = _kernel.Get<IMessenger>();
            if (messenger.ShowQuestion("Are you shure you want to fire this employee?"))
            {
                _session.FireEmployee(_employees[_view.SelectedEmployeeIndex]);
            }

            _employees = _session.GetEmployees();
            UpdateEmployeesTable();
        }

        private void OnHireEmployee()
        {
            var view = _kernel.Get<IAddEmployeeWindowView>();
            new AddEmployeeWindowPresenter(view, _session, _kernel);
            view.ShowWindow();

            _employees = _session.GetEmployees();
            UpdateEmployeesTable();
        }

        private void OnGetEmployees()
        {
            _employees = _session.GetEmployees();
            if (_employees.Count == 0)
            {
                var messenger = _kernel.Get<IMessenger>();
                messenger.ShowInformation("There are no employees.");
                return;
            }

            UpdateEmployeesTable();            
        }

        private void UpdateEmployeesTable()
        {
            var et = new DataTable();
            et.Columns.Add("Name");
            et.Columns.Add("LastName");
            et.Columns.Add("Patronymic");
            et.Columns.Add("Passport");
            et.Columns.Add("Phone");
            et.Columns.Add("Class");
            et.Columns.Add("Hire date");
            et.Columns.Add("Fire date");

            foreach (IEmployee employee in _employees)
            {
                var hireDate = "";
                if (employee.HireDate != DateTime.MinValue)
                {
                    hireDate = employee.HireDate.ToLongDateString();
                }
                var fireDate = "";
                if (employee.FireDate != DateTime.MinValue)
                {
                    fireDate = employee.FireDate.ToLongDateString();
                }

                var data = new object[]
                               {
                                   employee.Name, employee.LastName, employee.Patronymic, employee.Passport, employee.Phone, employee.Class.Description, hireDate, fireDate
                               };
                et.Rows.Add(data);
            }

            _view.EmployeesTable = et;          
        }

        private void OnFineClietns()
        {
            var clients = _session.GetFineClients();

            UpdateClientsTable(clients);
        }

        private void OnBestClients()
        {
            var clients = _session.GetBestClients();

            UpdateClientsTable(clients);
        }

        private void OnRegularClients()
        {
            var clients = _session.GetRegularClients();

            UpdateClientsTable(clients);
        }

        private void OnDeleteClient()
        {
            int selectedClientIndex = _view.SelectedClientIndex;
            if (selectedClientIndex == -1)
            {
                return;
            }

            var messenger = _kernel.Get<IMessenger>();
            if (messenger.ShowQuestion("Are you shure you want to delete this client?"))
            {
                _session.DeleteClient(_clients[selectedClientIndex].ClientId);
                UpdateClientsTable();
            }
        }

        private void OnAddClient()
        {
            var view = _kernel.Get<IAddClientWindowView>();
            new AddClientWindowPresenter(view, _session, _kernel);
            view.ShowWindow();

            UpdateClientsTable();
        }

        private void UpdateClientsTable()
        {
            _clients = _session.GetClients();

            UpdateClientsTable(_clients);
        }

        private void UpdateClientsTable(IList<IClient> clients)
        {
            _clients = _session.GetClients();

            var ct = new DataTable();
            ct.Columns.Add("Name");
            ct.Columns.Add("LastName");
            ct.Columns.Add("Patronymic");
            ct.Columns.Add("Passport");
            ct.Columns.Add("Phone");
            ct.Columns.Add("Discount");

            foreach (IClient client in clients)
            {
                var data = new object[]
                               {
                                   client.Name, client.LastName, client.Patronymic, client.Passport, client.Phone,
                                   client.Discount.Value
                               };
                ct.Rows.Add(data);
            }

            _view.ClientsTable = ct;
        }
    }
}